/*
 * Configuration.cpp
 *
 *  Created on: Sep 23, 2011
 *      Author: erudhes
 */
#include "Configuration.h"

namespace Configuration
{
/**
 * Vehicle class
 */

	Vehicle::Vehicle(wxXmlNode* node): mNode(node)
	{
		wxXmlNode* child = node->GetChildren();

		mName = node->GetAttribute(wxT("name"), wxT("name"));
		while(NULL != child)
		{
			if(child->GetName() == wxT("position"))
			{
				if(wxT("x") == child->GetAttribute(wxT("name"), wxT("name")))
					mPosition.x = wxAtoi(child->GetNodeContent().c_str());
				else if(wxT("y") == child->GetAttribute(wxT("name"), wxT("name")))
					mPosition.y = wxAtoi(child->GetNodeContent().c_str());
			}
			child = child->GetNext();
		}
	}

/**
 * Level class
 */

	Level::Level(wxXmlNode* node): mVehicleA(NULL), mVehicleB(NULL), mNode(node)
	{
		wxXmlNode* child = node->GetChildren();

		mName = node->GetAttribute(wxT("name"), wxT("name"));
		while(NULL != child)
		{
			if(child->GetName() == wxT("vehicle"))
			{
				Vehicle* vehicle = new Vehicle(child);

				if(wxT("A") == vehicle->getName())
					mVehicleA = vehicle;
				else if(wxT("B") == vehicle->getName())
					mVehicleB = vehicle;
			}
			child = child->GetNext();
		}
	}

/**
 * Levels class
 */

	Levels::Levels(wxXmlNode* node): mNode(node)
	{
		wxXmlNode* child = node->GetChildren();

		while(NULL != child)
		{
			if(child->GetName() == wxT("level"))
			{
				Level* level = new Level(child);
				mLevels[level->getName()] = level;
			}
			if(child->GetName() == wxT("path"))
			{
				mPath = child->GetNodeContent();
				// On Windows we need to use backslash as the path separator
#ifdef __WXMSW__
				mPath.Replace(wxT("/"), wxT("\\"), true);
#endif
			}
			child = child->GetNext();
		}
	}

/**
 * NetworkConfiguration class
 */

	NetworkConfiguration::NetworkConfiguration(wxXmlNode* node): mPortNumber(-1), mNode(node)
	{
		wxXmlNode* child = node->GetChildren();

		while(NULL != child)
		{
			if(child->GetName() == wxT("port-number"))
				mPortNumber = wxAtoi(child->GetNodeContent().c_str());
			child = child->GetNext();
		}
	}

/**
 * Configuration class
 */

	Configuration::Configuration(const wxString& configFile): mLogoPath(wxT("")), mDotSize(-1), mNetworkConfiguration(NULL), mLevels(NULL)
	{
		wxXmlNode* child = NULL;

		// Load the configuration file
		if(!mXmlConfig.Load(wxT("config.xml")))
			return;
		if(mXmlConfig.GetRoot()->GetName() != wxT("pixel-by-pixel"))
			return;
		child = mXmlConfig.GetRoot()->GetChildren();
		while(NULL != child)
		{
			if(child->GetName() == wxT("network"))
				mNetworkConfiguration = new NetworkConfiguration(child);
			if(child->GetName() == wxT("levels"))
				mLevels = new Levels(child);
			if(child->GetName() == wxT("logo"))
			{
				mLogoPath = child->GetNodeContent();
				// On Windows we need to use backslash as the path separator
#ifdef __WXMSW__
				mLogoPath.Replace(wxT("/"), wxT("\\"), true);
#endif
			}
			if(child->GetName() == wxT("dot-size"))
				mDotSize = wxAtoi(child->GetNodeContent().c_str());
			child = child->GetNext();
		}
	}
}
